package com.hndzyu.yd.task;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.barlibrary.ImmersionBar;
import com.hndzyu.yd.R;
import com.hndzyu.yd.base.BaseApplication;
import com.hndzyu.yd.customtask.ScreenshotFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zz9527 on 2018/10/11.
 */
public class TaskFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_task, container, false);
    }
    TabLayout tab;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initImmersionBar();
        ViewPager viewpager = view.findViewById(R.id.viewpager);
        tab = view.findViewById(R.id.tab);
        List<String> tabTitle = new ArrayList<>();
        tabTitle.add("简单任务");
        tabTitle.add("分享任务");
//        tabTitle.add("复杂任务");
        tabTitle.add("更多赚");
        List<Fragment> fragments = new ArrayList<>();
        TabTaskFragment tabTaskFragment = new TabTaskFragment();
        fragments.add(tabTaskFragment);
        fragments.add(TabShareFragment.newInstance(0));
//        fragments.add(TabFollowFragment.newInstance(0));
        fragments.add(new ScreenshotFragment());
        TabAdapter adapter = new TabAdapter(getFragmentManager(), fragments, tabTitle);
        //给ViewPager设置适配器
        viewpager.setAdapter(adapter);
        //将TabLayout和ViewPager关联起来。
        tab.setupWithViewPager(viewpager);
//        tabTaskFragment.setOnButtonClick(new TabTaskFragment.OnButtonClick() {
//            @Override
//            public void onClick(View view) {
//                tab.getTabAt(1).select();
//            }
//        });
        if (BaseApplication.starShare) {
            tab.getTabAt(1).select();
            BaseApplication.starShare=false;
        }
        tab.getTabAt(BaseApplication.taskposition).select();
    }



    public void selectTab(int position){
        tab.getTabAt(position).select();
    }
    protected ImmersionBar mImmersionBar;

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        mImmersionBar.statusBarColor(R.color.them)
                .init();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && mImmersionBar != null)
            ImmersionBar.with(this)
                    .statusBarColor(R.color.them)
                    .init();
    }

}

